package interfaze;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;

import logic.Mp3Player;
import logic.NoSaveException;
import logic.UIConfig;
import logic.Utilities;

/** Super Arcade 2 in 1 class.
 * @author Team 1
 *
 */
@SuppressWarnings("serial")
public class SuperArcade2in1 extends JFrame {
	
	protected boolean loadConfig = true;				//The flag to load UI the configuration.
	protected boolean isOnFullScreen = false;			//By default the JFrame is not on full screen.
	protected int width = 1280;							//JFrame's default width.
	protected int height = 720;							//JFrame's default height.
	protected final int FULLSCREEN_WIDTH = (int) Toolkit.getDefaultToolkit().getScreenSize().getWidth();	//Display's width.
	protected final int FULLSCREEN_HEIGHT = (int) Toolkit.getDefaultToolkit().getScreenSize().getHeight();	//Display's height.
	protected float xScale = width / 1280f;		//The proportion between JFrame's width and display's ones.
	protected float yScale = height / 720f;		//The proportion between JFrame's height and display's ones.
	protected GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();	//Graphics device.
	protected Mp3Player player = null;	//The music player.
	protected String userName;			//The logged user's nick.
	protected ArrayList<Object> list;	//The UIConfig attributes.
	
	/** SuperAcade2in1's constructor.
	 * @param userName
	 */
	SuperArcade2in1(String userName) {
		
		//Sets the logger user's nick name.
		this.userName = userName;
		
		//Try to loads the UI configuration of the user.
		try {
			list = UIConfig.load(this.userName);
		} catch (NoSaveException e) {
			//If there's no save sets the load flag to false.
			loadConfig = false;
		}
		if(loadConfig) {
			//If the user's UIConfig.dat exists, loads the last configuration saved.
			for(int i = 0; i < list.size(); i++) {
				Object configuration = list.get(i);
				if(list.get(i) instanceof String) {
					String resolution = (String) configuration;
					if(!this.isOnFullScreen) {
						//Gets the resolution saved on the file and sets the window's width and height with those values.
						Utilities.stringToResolutionParser(resolution, this);
					} else {
						//If the UIConfig.dat saved resolution is Full Screen, sets full screen mode.
						Utilities.windowToFullScreen(this);
					}
				} else if(list.get(i) instanceof Boolean) {
					if(i == 0) {
						//Sets if the window is on full screen or not.
						if(((Boolean) configuration).booleanValue()) {
							this.isOnFullScreen = true;
						} else {
							this.isOnFullScreen = false;
						}
					}
					if(i == 2) {
						if(((Boolean) configuration).booleanValue()) {
							//If the Config.dat saved that the decorated was enabled, the decoration will be set.
							this.dispose();
							this.setUndecorated(false);
							this.setVisible(true);
						} else {
							//If the Config.dat saved that the decorated wasn't enabled, the decoration will be removed.
							this.dispose();
							this.setUndecorated(true);
							this.setVisible(true);
						}
					}
				}
			}
		} else {
			//Sets the decoration's state as enabled.
			Boolean bDecoration = new Boolean(true);
			//If UIConfig.dat doesn't exists, generates a default configuration data and saves it as UIConfig table.
			logic.UIConfig.write(this.userName, false, "1280x720", bDecoration);
		}
		
		if(isOnFullScreen) {
			device.setFullScreenWindow(this);
		} else {
			this.setBounds((FULLSCREEN_WIDTH / 2) - (width / 2), (FULLSCREEN_HEIGHT / 2) - (height / 2), width, height);
		}
		
		//Sets the focus for mac or for windows.
		this.requestFocus();
		String osName = System.getProperty("os.name").toLowerCase();
		boolean isMacOs = osName.startsWith("mac os x");
		if(isMacOs) {
			JPanel panel = new GlobalMenuPanel(this);
			this.add(panel);
			panel.requestFocus();
		} else {
			JPanel panel = (JPanel) this.getContentPane();
			panel.add(new GlobalMenuPanel(this));
			panel.requestFocus();
		}
		
		//Set window's properties.
		this.setTitle("SuperArcadeGames 2 in 1");
		this.setResizable(false);
		this.setVisible(true);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	/** Returns the width of the JFrame.
	 * @param width : the actual width of the gaming JFrame.
	 */
	public void setWidth(int width) {
		this.width = width;
	}
	
	/** Returns the height of the JFrame.
	 * @param height : the actual height of the gaming JFrame.
	 */
	public void setHeight(int height) {
		this.height = height;
	}
	
	/** Updates the xScale if the windows width has been changed.
	 */
	public void updateXscale() {
		xScale = width / 1280f;
	}

	/** Updates the yScale if the windows height has been changed.
	 */
	public void updateyScale() {
		yScale = height / 720f;
	}
	
	/** Returns the display's width.
	 * @return FULLSCREEN_WIDTH: display's width.
	 */
	public int getFULLSCREEN_WIDTH() {
		return FULLSCREEN_WIDTH;
	}

	/** Returns the display's height.
	 * @return FULLSCREEN_HEIGHT: display's height.
	 */
	public int getFULLSCREEN_HEIGHT() {
		return FULLSCREEN_HEIGHT;
	}
	
	/** Sets if the JFrame is on full screen mode or not.
	 * @param isOnScreenMode : the flag to select if is on full screen mode.
	 */
	public void setOnFullScreen(boolean isOnScreenMode) {
		 this.isOnFullScreen = isOnScreenMode;
	}
	
	/** Return if the JFrame is on full screen mode or not.
	 * @return isOnFullScreen : a boolean that will be true if the window is on full screen mode, false otherwise.
	 */
	public boolean isOnFullScreen() {
		return isOnFullScreen;
	}
	
	/** Returns the graphics device.
	 * @return device : Display's graphic device.
	 */
	public GraphicsDevice getDevice() {
		return device;
	}
	
	/** Return the xScale.
	 * @return xScale : the proportion or JFrame's width and the display's ones.
	 */
	public float getXscale() {
		return xScale;
	}
	
	/** Return the yScale.
	 * @return yScale : the proportion or the JFrame's height and the display's ones.
	 */
	public float getYscale() {
		return yScale;
	}
	
	/** Returns the registered user's nick name.
	 * @return userName : the nick name of the registered user.
	 */
	public String getUserLogged() {
		return userName;
	}
	
	public static void main(String[] args) {
		new SuperArcade2in1("jesus");
	}
}
